package com.bawan.ddbb.dao;

import com.bawan.ddbb.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Desc:  UserDAO
 * Author: zhengcongchun
 * CreateTime: 2014-05-28 13:00
 * LastModifyTime: 2014-05-28 13:00
 * FinalModifier: zhengcongchun
 * Version: 1.0
 */
@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int getMatchCount(String username, String password) {
        String sql = "select count(*) from t_user where user_name=? and password=?";

        return jdbcTemplate.queryForInt(sql, new Object[]{username, password});
    }

    public User findUserByUsername(String username) {
        String sql = "select user_id, user_name, credits from t_user where user_name=?";

        final User user = new User();
        jdbcTemplate.query(sql, new Object[]{username}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setCredits(rs.getInt("credits"));
            }
        });

        return user;
    }

    public void updateLoginInfo(User user) {
        String sql = "update t_user set last_visit=?, last_visit_ip=?, credits=? where user_id=?";
        jdbcTemplate.update(sql, new Object[]{user.getLastVisit(), user.getLastIp(), user.getCredits(), user.getUserId()});
    }
}
